package com.evil.common.application.dto.permission.app;

import com.alibaba.fastjson.annotation.JSONField;
import com.evil.common.application.enums.permission.PermissionVisibleEnum;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Range;

import java.util.Objects;

/**
 * 应用权限
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Data
@NoArgsConstructor
public class AppPermissionDTO {

    /**
     * 权限类型(0:未知类型, 1: 用户, 2: 部门, 3: 角色)
     */
    @NotNull(message = "权限类型不能为空")
    @Range(min = 1, max = 8, message = "权限类型参数错误")
    private Integer permissionType;

    /**
     * 密码
     */
    private String password;

    /**
     * 权限可见类型(0:可使用, 1:仅可见, 2-不可见)
     */
    private Integer visible;

    /**
     * 目标id
     */
    @NotNull(message = "目标id不能为空")
    private Long targetId;

    /**
     * 目标
     */
    private Object target;

    /**
     * 源类型
     */
    @JSONField(serialize = false)
    private Integer sourceType;

    /**
     * 源ID
     */
    @JSONField(serialize = false)
    private Long sourceId;

    public AppPermissionDTO(Integer permissionType, Long targetId) {
        this.permissionType = permissionType;
        this.visible = PermissionVisibleEnum.AVAILABLE.getId();
        this.targetId = targetId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        AppPermissionDTO that = (AppPermissionDTO) o;
        return Objects.equals(permissionType, that.permissionType) &&
                Objects.equals(targetId, that.targetId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(permissionType, targetId);
    }
}
